<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="客户满意度" prop="satisfaction">
            <el-radio v-for="(item) in satisfactionList" :key="item.code" v-model="formItem.satisfaction" :label="item.code">{{ item.text }}</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件">
            <el-upload
              class="upload-demo"
              :headers="headers"
              :action="importUrl"
              :file-list="formItem.annex"
              :before-upload="handleBeforeUpLoad"
              :on-success="handleSuccess"
              :on-error="handleError"
              :on-remove="handleRemove"
              :show-file-list="true"
            >
              <el-button size="small" type="primary" icon="el-icon-upload2">上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div style="float:right">
            <el-button size="mini" @click="save">提交</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>

import { customerReviewCreate as create, customerReviewDetail as detail, customerReviewDeleteAnnexByFsId, customerReviewImportUrl } from '@neu/dcom-ui/api/malfunction'
// import { findByDictionaryType } from '@neu/dcom-ui/api/dictionary'
import { getToken } from '@neu/biz-core/utils/auth'

const formItem = {
  // 工作流TaskId
  taskId: '',
  // 工单号
  workNum: '',
  // 客户满意度
  satisfaction: '',
  // 附件列表
  annex: []
}

const formRules = {
  satisfaction: [{ required: true, message: '客户满意度不能为空', trigger: 'blur' }]
}

export default {
  name: 'CustomerReviewForm',
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: { ...formRules },
      /** 上传路径 */
      importUrl: customerReviewImportUrl,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      // 客户满意度
      satisfactionList: [
        { text: '满意', code: 'satisfied' },
        { text: '非常满意', code: 'verySatisfied' },
        { text: '基本满意', code: 'basicallySatisfied' },
        { text: '不满意', code: 'dissatisfied' },
        { text: '非常不满意', code: 'veryDissatisfied' }
      ]
    }
  },
  created() {
    this.fetch()
    // 客户满意度
    // findByDictionaryType('satisfaction').then(response => {
    //   this.satisfactionList = response.data
    // })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    save(whetherAgree) {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          // 设置工单号和任务
          this.formItem.taskId = this.taskId
          this.formItem.workNum = this.workOrderNum
          // 调用后台
          create(this.formItem).then(res => {
            if (res.status === 'success') {
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          })
        }
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    /** 文件上传成功 */
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '文件上传成功',
          type: 'success'
        })
        const obj = {
          fsId: response.data.fsId,
          name: response.data.name
        }
        const fsIdArr = this.formItem.annex.map(t => t.fsId)
        if (!fsIdArr.includes(obj.fsId)) {
          this.formItem.annex.push(obj)
        } else {
          fileList.pop()
        }
      }
    },
    /** 文件上传失败 */
    handleError(err, file, fileList) {
      console.log(err)
    },
    /** 文件移除 */
    handleRemove(file, fileList) {
      customerReviewDeleteAnnexByFsId(file.fsId).then(res => {
        if (res.status === 'failure') {
          this.$message({
            showClose: true,
            message: res.data,
            type: 'error'
          })
        } else {
          this.formItem.annex.splice(this.formItem.annex.indexOf(file), 1)
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
        }
      })
    },
    /** 文件上传之前 */
    handleBeforeUpLoad(file) {

    }
  }
}
</script>
